﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.ResumeService;
using System.Web.Security;
using AvatarHelper;
using Avatar.Model;
using System.Data.Linq;
using Avatar.Person;
using Avatar.MembershipService;

public partial class PersonResume : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PersonHelper.BasicInformationJobIntentionCheck();
        if (!IsPostBack)
        {
            var _user = LoginManager.GetCurrentUser();
            if (_user != null)
            {
                using (var _adc = DBHelper.GetDataContext())
                {
                    var _resume = _adc.Resume.Where(r => r.User == _user.UserName).FirstOrDefault();

                    if (_resume != null)
                    {
                        //基本信息
                        var _info = _resume.PersonalBasicInfo;
                        if (_info != null)
                        {
                            lbl_AccountLocation.Text = _info.Area.Name;
                            lbl_Birthday.Text = _info.Birthday.ToShortDateString();
                            lbl_Blog.Text = string.IsNullOrEmpty(_info.Blog) ? "None" : _info.Blog;
                            lbl_Blog.NavigateUrl = string.IsNullOrEmpty(_info.Blog) ? "#" : _info.Blog;
                            lbl_Blog.Target = "_blank";
                            if (!string.IsNullOrEmpty(_info.DocumentNumber))
                            {
                                lbl_DocumentNumber.Text = _info.DocumentNumber;
                                lbl_DocumentType.Text = " | "+_info.DocumentType.Document+":";
                            }
                            lbl_Email.Text = _info.Email;
                            lbl_Email.NavigateUrl = "mailto:" + _info.Email;
                            lbl_Email.Target = "_blank";
                            lbl_Height.Text = string.IsNullOrEmpty(_info.Height.ToString()) ? "" : " | 身高:" + _info.Height.ToString();
                            lbl_KeyWord.Text = string.IsNullOrEmpty(_info.KeyWord) ? "未填" : _info.KeyWord;
                            lbl_LivingCity.Text = _info.Area1.Name;
                            lbl_Marry.Text = _info.MaritalStatus == null ? string.Empty : " | " + _info.MaritalStatus.Status;
                            lbl_QQ.Text = string.IsNullOrEmpty(_info.QQ) ? "" : "QQ: " + _info.QQ;
                            lbl_Salary.Text = "当前年薪:" + (_info.SalaryYear == null ? "未填" : _info.SalaryYear.Salary);
                            lbl_Sex.Text = _info.SexID.ToString();
                            try
                            {
                                Convert.ToInt32(_info.TelNumber);
                                lbl_TelNumber.Text = string.IsNullOrEmpty(_info.TelNumber) ? "" : _info.TelNumber + "(手机) | ";
                            }
                            catch 
                            {
                                lbl_TelNumber.Text = "";
                            }
                                lbl_UserName.Text = _info.Name;
                            lbl_WorkingAge.Text = _info.WorkingAge.Age;
                            lbl_Sex.Text = _info.Sex.Name == "男女不限" ? "保密" : _info.Sex.Name;
                            lbl_ViewCount.Text = _resume.ViewCount.ToString();
                            lbl_UpdateTime.Text = Convert.ToDateTime(_resume.UpdateDate).ToShortDateString();
                            link_ApplyCount.Text = JobApplicationRecordManager.GetRecordList(_user.UserName).Count().ToString();
                            
                            //自我评价
                            if (_resume.SelfEvaluation == null)
                                panel_SelfEvaluation.Visible = false;
                            else
                                lbl_SelfEvaluation.Text = _resume.SelfEvaluation.Evaluation;

                        }

                        var _intention = _resume.JobIntention;
                        if (_intention != null)
                        {
                            lbl_JobType.Text = _intention.JobType.Name;
                            lbl_Area.Text = _intention.Area.Name;
                            foreach (var _i in _intention.JobIntentionIndustry)
                            {
                                lbl_Industry.Text += _adc.Industry.FirstOrDefault(i => i.ID == _i.IndustryID).Name + " ";
                            }

                            lbl_Position.Text = (_intention.Position == null || string.IsNullOrEmpty(_intention.PositionName)) ? "未设置" : _intention.Position.Name + "&nbsp&nbsp" + _intention.PositionName;
                            
                            lbl_SalaryWant.Text = _adc.ExpectationMoney.FirstOrDefault(em => em.ID == _intention.ExpectationMoney).Name;
                            lbl_TimeOnDuty.Text = _intention.TimeOnDuty.Name;
                            lbl_Attitude.Text = _intention.Attitude.JobAttitude;
                        }
                        if (_resume.WorkExperience.Count < 1)
                        {
                            panel_WorkExperience.Visible = false;
                        }
                        else
                        {
                            rpt_WorkExperience.DataSource = _resume.WorkExperience;
                            rpt_WorkExperience.DataBind();
                        }

                        if (_resume.Education.Count < 1)
                        {
                            panel_Education.Visible = false;
                        }
                        else
                        {
                            rpt_Education.DataSource = _resume.Education;
                            rpt_Education.DataBind();
                        }

                        if (_resume.TrainingExperience.Count < 1)
                        {
                            panel_Training.Visible = false;
                        }
                        else
                        {
                            rpt_TrainingExperience.DataSource = _resume.TrainingExperience;
                            rpt_TrainingExperience.DataBind();
                        }

                        if (_resume.LanguageAbility.Count < 1)
                        {
                            panel_LanguageAbility.Visible = false;
                        }
                        else
                        {
                            rpt_Language.DataSource = _resume.LanguageAbility;
                            rpt_Language.DataBind();
                        }

                        if (_resume.LanguageLevel != null)
                        {
                            lbl_English.Text = _resume.LanguageLevel.EnglishLevel.Level;
                            lbl_Toefl.Text = _resume.LanguageLevel.TOEFL + "分";
                            lbl_Gre.Text = _resume.LanguageLevel.GRE + "分";
                            lbl_Japanese.Text = _resume.LanguageLevel.JapaneseLevel.Level;
                            lbl_Gmat.Text = _resume.LanguageLevel.GMAT + "分";
                            lbl_Ielts.Text = _resume.LanguageLevel.IELTS + "分";
                        }
                        else
                        {
                            panel_language.Visible = false;
                        }
                    }
                }
            }
        }
    }
    protected void btn_EducationDel_Click(object sender, EventArgs e)
    {

    }

    protected void btn_TrainingExperienceUpdate_Click(object sender, EventArgs e)
    {
        Button _b = sender as Button;

        Response.Redirect("./TrainingExperience.aspx?ID=" + _b.CommandArgument);
    }
    protected void btn_EducationUpdate_Click(object sender, EventArgs e)
    {
        Button _b = sender as Button;

        Response.Redirect("./Education.aspx?ID=" + _b.CommandArgument);
    }
    protected void btn_LanguageDel_Click(object sender, EventArgs e)
    {

    }
    protected void btn_LanguageUpdate_Click(object sender, EventArgs e)
    {
        Button _b = sender as Button;

        Response.Redirect("./Language.aspx?ID=" + _b.CommandArgument);
    }
    protected void btn_AddEducation_Click(object sender, EventArgs e)
    {

    }
    protected string GetDate(DateTime _beginDate, DateTime _overDate)
    {
        string _b, _o;
        _b = _beginDate.Year + "年" + _beginDate.Month + "月 至 ";
        if (_overDate == DateTime.MinValue)
            _o = "现在";
        else
            _o = _overDate.Year + "年" + _overDate.Month + "月";

        return _b + _o;
    }
    protected void btn_EducationAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("./PersonEducation.aspx");
    }
    protected void btn_LanguageAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("./PersonLanguage.aspx");
    }
    protected void btn_WorkExperience_Click(object sender, EventArgs e)
    {
        Response.Redirect("./PersonWorkExperience.aspx");
    }
    protected void btn_WorkExperienceUpdate_Click(object sender, EventArgs e)
    {
        Button _b = sender as Button;
        Response.Redirect("./PersonWorkExperience.aspx?id=" + Convert.ToInt32(_b.CommandArgument));
    }
    protected void btn_TrianingAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("./PersonTrainingExperience.aspx");
    }
    protected void btn_Update_Click(object sender, EventArgs e)
    {
        Response.Redirect("./PersonLanguage.aspx");
    }
}
